import {
  ajax,
  src,
  util
} from "../../utils/util.js";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 0 列表 1地图
    moshi: 0,
    yijiindex: 0,
    erjiindex: -1,
    jibieindex: -1,
    paixuindex: 1,
    showaddresstc: false,
    list: '11',
    longitude: 0,
    latitude: 0,
    detailsindex:'',
    guanggao:'',

   
    
    canjiazai: true,

    islogin: false,
  },

  //立即获取智能康复计划  && 授权
  bindGetUserInfo(e) {
    var that = this;
    console.log(e.detail.userInfo)
    var id = e.target.dataset.id
    console.log(id)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      if (that.data.islogin == false) {
        

        var code = wx.getStorageSync("code");
        ajax('/api/index/login', {
          data: {
            js_code: code,
            nickname: e.detail.userInfo.nickName,
            avatar: e.detail.userInfo.avatarUrl
          },
          header: {
            'Accept': 'application/json', // 默认值
          },
        }, res => {
          console.log(res)
          if (res.error == 0) {
            wx.setStorageSync("uqid", res.data.uqid)
            that.setData({
              uqid: res.data.uqid,
              session_key: res.data.session_key
            });
            wx.navigateTo({
              url: '../details/details?id='+id +'&type=1',
            })

          } else {
            wx.showToast({
              title: res.message,
              icon: 'none',
              duration: 2000
            })

          }
        })
      }else{
        wx.navigateTo({
          url: '../details/details?id='+id +'&type=1',
        })
      }
    } else {
      // wx.navigateTo({
      //   url: '../details/details?id='+1 +'&type=1',
      // })
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }
  },

  tokaiqi: function () {
    console.log("开启定位")
  },

  tosearchjigou: function () {
    wx.navigateTo({
      url: '/pages/searchjigou/searchjigou',
    })
  },

  tomap: function () {
    // console.log(this.data.mdetails.longitude)
    // wx.navigateTo({
    //   url: '/pages/map/map?longitude=' + this.data.mdetails.longitude + "&latitude=" + this.data.mdetails.latitude,
    // })
    var detailsindex = this.data.detailsindex
    var latitude = Number(this.data.list[detailsindex].latitude)
    var longitude = Number(this.data.list[detailsindex].longitude)
    var name = this.data.list[detailsindex].name
    var address = this.data.list[detailsindex].address
    wx.openLocation({
      latitude,
      longitude,
      scale: 18,
      name,
      address
    })
  },


  // 选地区
  select: function (e) {
    var index = e.currentTarget.dataset.index;
    if (this.data.showtc == false) {
      this.setData({
        select: Number(index)
      })
      this.setData({
        showtc: true
      })
    } else {
      if(this.data.select== Number(index)){
        this.setData({
          showtc: false
        })
      }else{
        this.setData({
          select: Number(index)
        })
        this.setData({
          showtc: true
        })
      }
    }
  },

  // 选择级别
  selectjb: function (e) {
    var index = e.currentTarget.dataset.index;
    if (index == -1) {

    }

    this.setData({
      jibieindex: Number(index),
      detailsindex: '',
    })
   
    this.setData({
      showtc: false
    })
   
    this.getlist()
  },

  // 选择排序
  selectpx: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      paixuindex: Number(index),
      currentpage: 1,
      nogd: false,
      canjiazai: true,
    })
    this.setData({
      showtc: false
    })


    this.getlist()
  },


  getalldiqu: function () {
    ajax("/api/index/area_list", {
      data: {},
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {

      var obj = {
        name: '全部地区',
        province_id: "1",
        list: [{
          name: "全部地区",
          city_id: "1"
        }]
      }
      res.data.unshift(obj)

      this.setData({
        alldiqu: res.data
      })
    })
  },

  getguanggao: function () {
    ajax("/api/index/config_show", {
      data: {
        key:'jc_image'
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {

      this.setData({
        guanggao: res.data
      })
    })
  },

   // 获取全部分类
   getfenleilist: function () {
   

    ajax("/api/index/mt_type", {
      data: {
        
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      this.setData({
        fenleilist: res.data
      })
     
    })
  },

  // 获取全部列表
  getlist: function () {
    var yijiindex = this.data.yijiindex
    var erjiindex = this.data.erjiindex
    var province_id
    var city
    var mtid
    var that=this

    if (yijiindex == 0) {
      province_id = ''
      city = ''
    } else if (erjiindex == -1) {
      province_id = that.data.alldiqu[yijiindex].province_id
      city = ''
    } else {

      province_id = that.data.alldiqu[yijiindex].province_id
      city = that.data.alldiqu[yijiindex].list[erjiindex].city_id
    }

    if(that.data.jibieindex==-1){
      mtid= ''
    }else{
      mtid= that.data.fenleilist[that.data.jibieindex].id
    }

    ajax("/api/index/mechanisms_list", {
      data: {
        lng: that.data.longitude,

        lat: that.data.latitude,
        province_id: province_id,
        city: city,
        mtid:mtid,
        sort: that.data.paixuindex,
        is_map: that.data.moshi
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      if(res.error==0){
        if(res.data){
          if(that.data.moshi==0){
            that.setData({
              list: res.data
            })
          }
          if(that.data.moshi==1){

            var list=[]
            for(let x=0;x<res.data.length;x++){
              for(let y=0;y<res.data[x].list.length;y++){
                list.push(res.data[x].list[y])

                var obj ={
                  content: '',
                  color: 'transparent',
                  fontSize: 0,
                  display: 'BYCLICK',
                }
                res.data[x].list[y].width ='30px'
                res.data[x].list[y].height = '46px'
                if( res.data[x].list[y].mtid==1){
                  res.data[x].list[y].iconPath='../images/dituicon1.png'
                }
                if( res.data[x].list[y].mtid==2){
                  res.data[x].list[y].iconPath='../images/dituicon2.png'
                }
                if( res.data[x].list[y].mtid==3){
                  res.data[x].list[y].iconPath='../images/dituicon3.png'
                }
                res.data[x].list[y].callout=obj
              }
            }
            that.setData({
              list: list
            })
          }
        }else{
          that.setData({
            list: []
          })
        }
        
      }
     
    })
  },

  //点击控件时
  bianhuaview: function (e) {
    // console.log(e)
    this.setData({
      detailsindex: ''
    })
  },

  //选择地区
  selectdiqu: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      yijiindex: index,
      erjiindex: -1,

    })

  },
  //选择具体地区
  selectjtdiqu: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      erjiindex: Number(index),
      detailsindex: '',
      currentpage: 1,
      nogd: false,
      canjiazai: true,
    })
    this.setData({
      showtc: false
    })
    this.getlist()
  },

  // 切换模式
  switchmoshi: function () {

    if (this.data.moshi == 0) {
      var that = this
      wx.getSetting({
        success(res) {
          console.log(res.authSetting['scope.userLocation'])
          if (res.authSetting['scope.userLocation']) {
            that.setData({
              showaddresstc: false
            })
            wx.getLocation({
              type: 'wgs84',
              success: (res) => {
                var latitude = res.latitude
                var longitude = res.longitude
                var speed = res.speed
                var accuracy = res.accuracy

                wx.setStorageSync("latitude", latitude)
                wx.setStorageSync("longitude", longitude)

                that.qiehuan()

              },
            })
          } else {
            that.setData({
              showaddresstc: true
            })
          }
        }
      })
    } else {
      this.qiehuan()
    }
  },

  qiehuan: function () {

    if (this.data.moshi == 0) {
      this.setData({
        latitude: wx.getStorageSync('latitude'),
        longitude: wx.getStorageSync('longitude')
      })
      this.setData({
        moshi: 1,
        showtc: false,
      })
      wx.setStorageSync('moshi', 1)
      this.getlist()
    } else {
      this.setData({
        moshi: 0,
        showtc: false,
       
      })
      wx.setStorageSync('moshi', 0)
      this.getlist()
    }
  },

  changeModalCancel: function () {
    this.setData({
      showaddresstc: false
    })
  },

  markertap: function (e) {
    console.log(e.detail.markerId)
    var detailsindex
    for (var x in this.data.list) {
      if (this.data.list[x].id == e.detail.markerId) {
        detailsindex = x
      }
    }

    this.setData({
      detailsindex: detailsindex
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.getlist()
    this.getguanggao()
    this.getfenleilist()
    this.getalldiqu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var uqid = wx.getStorageSync('uqid');
    console.log(uqid)
    var moshi = wx.getStorageSync('moshi');
    this.setData({
      uqid: uqid
    })
    if (uqid == '' || uqid == undefined) {
      this.setData({
        islogin: false
      });
    } else {
      this.setData({
        islogin: true
      });
    }
    // if(moshi){
    //   this.setData({
    //     moshi:moshi
    //   })
    // }



    var that=this
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy

        wx.setStorageSync("latitude", latitude)
        wx.setStorageSync("longitude", longitude)
        that.setData({
          latitude: wx.getStorageSync('latitude'),
          longitude: wx.getStorageSync('longitude')
        })
        that.getlist()
      },
      fail: (res) => {
        this.setData({
          showaddresstc: true
        })
      },
    })

  },
  setjingwei: function () {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        wx.setStorageSync("latitude", latitude)
        wx.setStorageSync("longitude", longitude)

        that.setData({
          latitude: wx.getStorageSync('latitude'),
          longitude: wx.getStorageSync('longitude')
        })
        that.getlist()
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      showtc:false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})